﻿using System;
using System.Collections.Generic;
using System.Text;

using formworks.DBO.Attributes;

namespace formworks.DBO.Core
{
    public class Toolkit
    {
        #region 数据库相关方法

        /// <summary>
        /// 获取实体类中标识的主键名称
        /// 例：
        /// getPrimaryKey(propertys[0].GetCustomAttributes(typeof(PrimaryKey), false));
        /// </summary>
        /// <param name="attributes">需要查询的 attribute</param>
        /// <returns>主键那名称</returns>
        public static string getPrimaryKey(object[] attributes)
        {
            try
            {
                //string head = "";
                //string tail = "";
                foreach (Constraints item in attributes)
                {
                    if (Constraints.Types.PrimaryKey == item.keyType)
                    {
                        return item.keyValue;
                    }
                    //head = item.primaryKeyType.Substring(0, 3);
                    //tail = item.primaryKeyType.Substring(3,
                    //    item.primaryKeyType.Length - 3);
                    //if ("PK_" == head)
                    //    return tail;
                }

                return null;
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
        }


        /// <summary>
        /// 这个类对可空类型进行判断转换，
        /// 要不然会报错  
        /// </summary>
        /// <param name="value">要检测的值</param>
        /// <param name="conversionType">要变化的类型</param>
        /// <returns></returns>
        public static object checkType(object value, Type conversionType)
        {
            if (conversionType.IsGenericType
                && conversionType.GetGenericTypeDefinition().Equals(
                typeof(Nullable<>))
                )
            {
                if (value == null)
                    return null;

                System.ComponentModel.NullableConverter nullableConverter =
                    new System.ComponentModel.NullableConverter(conversionType);
                conversionType = nullableConverter.UnderlyingType;
            }
            return Convert.ChangeType(value, conversionType);
        }

        /// <summary>
        /// 是空或者dbnull
        /// </summary>
        /// <param name="obj">要判定的对象</param>
        /// <returns></returns>
        public static bool IsNullOrDBNull(object obj)
        {
            return ((obj is DBNull) || string.IsNullOrEmpty(obj.ToString())) ? true : false;
        }


        #region 实体类到数据库互相转换方法

        /// <summary>
        /// 数据库和实体类的差异转化
        /// 数据库对象 转换到 实体类
        /// 把下斜杠转化为大写
        /// 例: user_name -> userName
        /// </summary>
        /// <param name="fieldName">字段字符串</param>
        /// <returns></returns>
        public static string data2entity(string fieldName)
        {
            if (fieldName.IndexOf('_') < 1)
                return fieldName;

            string m_src = fieldName;
            string m_des = "";

            char[] src_char = m_src.ToCharArray();
            for (int i = 0; i < src_char.Length; i++)
            {

                if ('_' == src_char[i])
                {
                    m_des += Char.ToUpper(src_char[i + 1]);
                    ++i;//
                    continue;
                }
                m_des += src_char[i];
            }//end find

            return m_des;
        }

        /// <summary>
        /// 实体类和的数据库差异转化
        /// 实体类 转换到 数据库对象
        /// 把首字母除外的大写转化为下斜杠+小写字符
        /// 例: userName -> user_name
        /// </summary>
        /// <param name="fieldName">字段字符串</param>
        /// <returns></returns>
        public static string entity2data(string fieldName)
        {
            string m_src = fieldName;
            //its lower
            if (m_src.ToLower().Equals(fieldName))
                return fieldName;

            string m_des = "";
            char and = '_';

            char[] src_char = m_src.ToCharArray();
            for (int i = 0; i < src_char.Length; i++)
            {
                if (Char.IsUpper(src_char[i]))
                {
                    m_des += and;
                    m_des += Char.ToUpper(src_char[i]);
                    continue;
                }
                m_des += src_char[i];
            }//end find

            return m_des;
        }
        #endregion

        #endregion

       
    }//end class
}//end namespace
